package com.ruoyi.web.controller.basic;

import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.basic.domain.JySemester;
import com.ruoyi.basic.service.JySemesterService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 学期Controller
 *
 * @author ruoyi
 * @date 2024-04-05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basic/semester")
public class SemesterController extends BaseController {
    private final JySemesterService jySemesterService;

    /**
     * 查询学期列表
     */
    @PreAuthorize("@ss.hasPermi('semester:semester:list')")
    @GetMapping("/list")
    public TableDataInfo list(JySemester jySemester) {
        startPage();
        List<JySemester> list = jySemesterService.selectJySemesterList(jySemester);
        return getDataTable(list);
    }

    /**
     * 导出学期列表
     */
    @PreAuthorize("@ss.hasPermi('semester:semester:export')")
    @Log(title = "学期", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JySemester jySemester) throws IOException {
        List<JySemester> list = jySemesterService.selectJySemesterList(jySemester);

        // Step.3 调用AutoPoi导出Excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String sheetName = "学期数据";
        String fileName = URLUtil.encode(sheetName).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), JySemester.class).sheet(sheetName).doWrite(list);
    }

    /**
     * 获取学期详细信息
     */
    @PreAuthorize("@ss.hasPermi('semester:semester:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jySemesterService.selectJySemesterById(id));
    }

    /**
     * 新增学期
     */
    @PreAuthorize("@ss.hasPermi('semester:semester:add')")
    @Log(title = "学期", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JySemester jySemester) {
        return toAjax(jySemesterService.insertJySemester(jySemester));
    }

    /**
     * 修改学期
     */
    @PreAuthorize("@ss.hasPermi('semester:semester:edit')")
    @Log(title = "学期", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JySemester jySemester) {
        return toAjax(jySemesterService.updateJySemester(jySemester));
    }

    /**
     * 修改学期
     */
    @Log(title = "学期", businessType = BusinessType.UPDATE)
    @PutMapping("semester_current")
    public AjaxResult updateSemesterCurrent(@RequestBody JSONObject jsonObject) {
        Long id = jsonObject.getLong("id");
        Byte current = jsonObject.getByte("current");
        return toAjax(jySemesterService.updateSemesterCurrent(id, current));
    }

    /**
     * 删除学期
     */
    @PreAuthorize("@ss.hasPermi('semester:semester:remove')")
    @Log(title = "学期", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jySemesterService.deleteJySemesterByIds(ids));
    }
}
